package com.cal.calonecal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cal.calonecal.common.Response;
import com.cal.calonecal.pojo.entity.UserInfo;
import com.cal.calonecal.service.IUserInfoService;
import com.cal.calonecal.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.time.LocalDateTime;

/**
 * @author Binary Tree Cat
 */
@RestController("/user")
@CrossOrigin
public class UserInfoController {

    @Autowired
    private IUserInfoService iUserInfoService;

    @Autowired
    RedisTemplate redisTemplate;

    @PutMapping ("/login")
    public Response<UserInfo> login(@RequestBody UserInfo userInfo, HttpServletResponse httpServletResponse){
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userInfoLambdaQueryWrapper.eq(UserInfo::getUsername, userInfo.getUsername());
        UserInfo one = iUserInfoService.getOne(userInfoLambdaQueryWrapper);
        if (one == null){
            return Response.error("用户不存在");
        } else{
            if (one.getPassword().equals(userInfo.getPassword())) {
                ValueOperations valueOperations = redisTemplate.opsForValue();
                Object o = valueOperations.get(one.getId());
                if (o == null){
                    valueOperations.set(one.getId(),one.getUsername());
                }
//                httpServletResponse.setHeader("Authorization", TokenUtil.generateToken(String.valueOf(userInfo.getId())));
                return Response.success(one);
            } else {
                return Response.error("密码错误");
            }
        }
    }

    @PutMapping ("/logout")
    public R logout(@RequestParam Integer userId){
        redisTemplate.delete(userId);
        return R.ok("退出成功");
    }

    @PutMapping("/register/update")
    public Response register(@RequestBody UserInfo userInfo){
        userInfo.setCreateTime(LocalDateTime.now());
        userInfo.setType(0);
        if(iUserInfoService.count(new QueryWrapper<UserInfo>().eq("username", userInfo.getUsername())) == 0){
            iUserInfoService.saveOrUpdate(userInfo);
        }else return Response.error("用户名已存在");

        return Response.success(userInfo);
    }

    @GetMapping("/select-page")
    public Response<IPage<UserInfo>>selectUserPage(Integer pageNum, Integer pageSize){
        return Response.success(iUserInfoService.page(new Page<UserInfo>(pageNum, pageSize)));
    }

    @DeleteMapping("/delete")
    public Response<Void>delete(Long uid){
        iUserInfoService.removeById(uid);
        return Response.success();
    }


    @GetMapping("/select-by-username")
    public Response<IPage<UserInfo>>selectByName(String username, Integer pageNum, Integer pageSize){
        return Response.success(iUserInfoService.page(new Page<UserInfo>(pageNum, pageSize),
                new QueryWrapper<UserInfo>().like("username","%"+username+"%")));
    }


}
